package hn.sigit.view.workflow.permits;

import hn.sigit.dao.hnd.administrative.HND_BuildingPermitDAO;
import hn.sigit.dao.hnd.administrative.HND_OperationPermitDAO;
import hn.sigit.dao.hnd.administrative.HND_PermitRuleGroupDAO;
import hn.sigit.model.hnd.administrative.HND_OperationPermit;
import hn.sigit.model.hnd.administrative.HND_OperationPermitType;
import hn.sigit.model.hnd.administrative.HND_PermitRuleGroup;
import hn.sigit.model.hnd.administrative.HND_PermitType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.bpm.BeginTask;
import org.jboss.seam.annotations.bpm.CreateProcess;
import org.jboss.seam.annotations.bpm.EndTask;
import org.jboss.seam.bpm.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;


@Name("operationPermitHelper")
@Scope(value=ScopeType.CONVERSATION)
@AutoCreate
public class OperationPermitHelper extends PermitHelper implements Serializable {
	private static final long serialVersionUID = -5452696846360406096L;
	
	private static List<SelectItem> permitTypeValueList;
	
	private final String OPERATION_PERMIT_PROCESS = "operationPermitWorkflow";

	
	public HND_OperationPermit getPermit() {
		if (permit == null) {
			permit = new HND_OperationPermit();
		}
		return (HND_OperationPermit) permit;
	}
	public void setPermit(HND_OperationPermit permit) {
		this.permit = permit;
	}
	
	public List<TaskInstance> getTaskInstanceList() {
		return getTaskInstanceList(OPERATION_PERMIT_PROCESS);
	}

	public List<TaskInstance> getPooledTaskInstanceList() {
		return getPooledTaskInstanceList(OPERATION_PERMIT_PROCESS);
	}
	
	
	@End
	@CreateProcess(definition=OPERATION_PERMIT_PROCESS, processKey="#{operationPermitHelper.permitId}")
	@Transactional
	@Override
	public String beginProcess() {
		HND_OperationPermit hndPermit = getPermit();
		
		beginProcessAux(hndPermit);

		HND_BuildingPermitDAO.save(hndPermit);
		setPermitId(hndPermit.getId());
		

		return "operation_permit_activities";
	}


	@Override
	@BeginTask
	@Begin(join=true)
	public String startTask() {
		String taskName = org.jboss.seam.bpm.TaskInstance.instance().getTask().getName();
		setPermit(HND_OperationPermitDAO.loadOperationPermitByID(
				Long.parseLong(ProcessInstance.instance().getKey())));
		
		setPermitId(this.permit.getId());
		setParty(this.permit.getExtParty());
		
		return taskName;
	}

	@EndTask(transition="approval_back2analysis")
	public String approvalBack2Analysis() {
		return "home";
	}
	
	public List<SelectItem> getPermitTypeValueList() {
		if (permitTypeValueList == null) {
			permitTypeValueList = new ArrayList<SelectItem>();
			permitTypeValueList.add(new SelectItem(null, "(seleccionar)"));
			for (HND_OperationPermitType pt : HND_OperationPermitType.values())
				permitTypeValueList.add(new SelectItem(pt, resBundle.loadMessage(pt.name())));
		}
		return permitTypeValueList;
	}
	
	@Override
	public List<HND_PermitRuleGroup> getPermitRuleGroupList() {
		if (permitRuleGroupList == null) {
			permitRuleGroupList = HND_PermitRuleGroupDAO.loadPermitRuleGroupsByType(HND_PermitType.OPERATION_PERMIT);
		}
		return permitRuleGroupList;
	}
	@Override
	public List<SelectItem> getPermitRuleGroupItemList() {
		// TODO Auto-generated method stub
		return null;
	}

}
